#include "do_sql.h"
#include <sstream>
#include "yxjmj_conf.h"

using namespace std;
using namespace sql;

int get_server_info(int server_id)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
	Connection *con = mysql_instance->GetConnect();
	if (con == NULL)
	{
		GAME_LOG.error("get connect error.\n");
		return -1;
	}
	string sql = "SELECT * FROM t_server_info WHERE id = ? LIMIT 1;";
	stringstream out_sql;
	out_sql << sql << "(p1)" << server_id;
	try 
	{
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, server_id);
		ResultSet* result = prep->executeQuery();
		if (result->next())
		{
			ret = 0;
			YXJMJ_CONF["log"]["level"] = result->getInt("log_level");
			YXJMJ_CONF["game"]["host"] = result->getString("server_ip").asStdString();
			YXJMJ_CONF["game"]["port"] = result->getInt("server_port");
			YXJMJ_CONF["game"]["ws_port"] = result->getInt("websocket_port");
			YXJMJ_CONF["game"]["game_flag"] = result->getInt("server_flag");
			YXJMJ_CONF["game"]["min_access"] = result->getInt("min_access");
			YXJMJ_CONF["game"]["max_access"] = result->getInt("max_access");
			YXJMJ_CONF["game"]["base_gold"] = result->getInt("base_gold");
			YXJMJ_CONF["game"]["tai_fee"] = result->getInt("tai_fee");
		}
		if (result)delete result;
	}
	catch (sql::SQLException &e)
	{
		sql = out_sql.str();
		GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n", sql.c_str(), e.what());
		ret = -1;
	}
	catch (...)
	{
		GAME_LOG.error(" sql unknow error.\n");
		ret = -1;
	}

	return ret;
}

int get_db_player_info(S_DB_PLAYER_INFO &db_player_info, int uid)
{	
	int ret = -1;
    auto mysql_instance = MYSQL_CONN;
    Connection *con = mysql_instance->GetConnect();
    if (con == NULL)
    {
        GAME_LOG.error("get connect error.\n");
        return -1;
    }
	std::string sql = "SELECT * FROM t_player_info WHERE id = ? LIMIT 1;";
	stringstream out_sql;
	out_sql << sql << "(p1)" << uid;
    try
    {
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, uid);
		auto result = prep->executeQuery();
        //遍历结果集
        if(result->next())
        {
			db_player_info.id = result->getInt("id");			
			db_player_info.status = result->getInt("status");
			//db_player_info.user = result->getString("qhone_num");			
			db_player_info.login_ip = result->getString("login_ip");
			db_player_info.nick_name = result->getString("nick_name");	
			db_player_info.robot_flag = result->getInt("robot_flag");
			db_player_info.sex = result->getInt("sex");			
			db_player_info.avater_url = result->getString("avater_url");
			db_player_info.diamonds = result->getInt("diamonds");
			db_player_info.gold = result->getInt("gold");
			db_player_info.paiwei_score = result->getInt("paiwei_score");
			GAME_LOG.debug("get_db_player_info. id:%d,status:%d,login_ip:%s,nick_name:%s,robot_flag:%d,sex:%d,avater_url:%s,diamonds:%d,gold:%d,paiwei_score:%d.\n",
				db_player_info.id,
				db_player_info.status,
				db_player_info.login_ip.c_str(),
				db_player_info.nick_name.c_str(),
				db_player_info.robot_flag,
				db_player_info.sex,
				db_player_info.avater_url.c_str(),
				db_player_info.diamonds,
				db_player_info.gold,
				db_player_info.paiwei_score);
				ret = 0;
        }
        //查完后手动删除内存
        if (result){delete result;}
    }
    catch(sql::SQLException &e)
    {
		sql = out_sql.str();
         GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n",  sql.c_str(), e.what()); 
		 ret = -1;
    }
    catch(...)
    {
         GAME_LOG.error(" sql unknow error.\n");
		 ret = -1;
    }

	
	return ret;
}

int update_db_player_online(int uid, int if_game, int if_room, int if_online)
{
	int ret = -1;
    auto mysql_instance = MYSQL_CONN;
    Connection *con = mysql_instance->GetConnect();
    if (con == NULL)
    {
        GAME_LOG.error("get connect error.\n");
        return ret;
    }
	std::string sql = "UPDATE t_player_info SET if_game = ?, if_online = ?, if_room = ? WHERE id = ?;";
	stringstream out_sql;
	out_sql << sql << "(p1)" << if_game << "(p2)" << if_online << "(p3)" << if_room << "(p4)" << uid;
    try
    {
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, if_game);
		prep->setInt(2, if_online);
		prep->setInt(3, if_room);
		prep->setInt(4, uid);
		int change_num = prep->executeUpdate();
		if (change_num == 1)
		{
			ret = 0;
		}
    }
    catch(sql::SQLException &e)
    {
		sql = out_sql.str();
		GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n",  sql.c_str(), e.what()); 
		ret = -1;
    }
    catch(...)
    {
		GAME_LOG.error(" sql unknow error.\n");
		ret = -1;
    }

	return ret;
}

int decrease_gold(int uid, int num)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
    Connection *con = mysql_instance->GetConnect();
	
    if (con == NULL)
    {
        GAME_LOG.error("get connect error.\n");
        return ret;
    }
    std::string sql = "UPDATE t_player_info SET gold = gold + ? WHERE id = ?;";
	stringstream out_sql;
	out_sql << sql << "(p1)" << num << "(p2)" << uid;
    try
    {
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, num);
		prep->setInt(2, uid);
		if (prep->executeUpdate() == 1)
		{
			ret = 0;
		}
    }
    catch(sql::SQLException &e)
    {
		sql = out_sql.str();
         GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n",  sql.c_str(), e.what()); 
		 ret = -1;
    }
    catch(...)
    {
         GAME_LOG.error(" sql unknow error.\n");
		 ret = -1;
    }
	return ret;
}


int decrease_diamonds(int uid, int num)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
    Connection *con = mysql_instance->GetConnect();
	
    if (con == NULL)
    {
        GAME_LOG.error("get connect error.\n");
        return ret;
    }
	std::string sql = "UPDATE t_player_info SET diamonds = diamonds + ? WHERE id = ?;";
	stringstream out_sql;
	out_sql << sql << "(p1)" << num << "(p2)" << uid;
	try
	{
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, num);
		prep->setInt(2, uid);
		if (prep->executeUpdate() == 1)
		{
			ret = 0;
		}
	}
    catch(sql::SQLException &e)
    {
		sql = out_sql.str();
         GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n",  sql.c_str(), e.what()); 
		 ret = -1;
    }
    catch(...)
    {
         GAME_LOG.error(" sql unknow error.\n");
		 ret = -1;
    }
	return ret;
}

int decrease_paiwei_score(int uid, int add_score)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
	Connection *con = mysql_instance->GetConnect();

	if (con == NULL)
	{
		GAME_LOG.error("get connect error.\n");
		return ret;
	}
	std::string sql = "UPDATE t_player_info SET paiwei_score = paiwei_score + ? WHERE id = ?;";
	stringstream out_sql;
	out_sql << sql << "(p1)" << add_score << "(p2)" << uid;
	try
	{
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, add_score);
		prep->setInt(2, uid);
		int change_num = prep->executeUpdate();
		if (change_num == 1)
		{
			ret = 0;
		}
	}
	catch (sql::SQLException &e)
	{
		sql = out_sql.str();
		GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n", sql.c_str(), e.what());
		ret = -1;
	}
	catch (...)
	{
		GAME_LOG.error(" sql unknow error.\n");
		ret = -1;
	}
	return ret;
}

int insert_paiwei_score_change_rec(int uid, int change_begin, int change, int change_end, string source)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
	Connection *con = mysql_instance->GetConnect();

	if (con == NULL)
	{
		GAME_LOG.error("get connect error.\n");
		return ret;
	}
	std::string sql = "INSERT INTO t_user_paiwei_rec SET uid = ?, begin_num = ?, num = ?, end_num = ?, source = ?";
	stringstream out_sql;
	out_sql << sql << "(p1)" << uid
		<< "(p2)" << change_begin
		<< "(p3)" << change
		<< "(p4)" << change_end
		<< "(p5)" << source;
	try
	{
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, uid);
		prep->setInt(2, change_begin);
		prep->setInt(3, change);
		prep->setInt(4, change_end);
		prep->setString(5, source);
		if (prep->execute())
		{
			ret = 0;
		}
	}
	catch (sql::SQLException &e)
	{
		sql = out_sql.str();
		GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n", sql.c_str(), e.what());
		ret = -1;
	}
	catch (...)
	{
		GAME_LOG.error(" sql unknow error.\n");
		ret = -1;
	}
	return ret;
}



/*
int insert_cur_online_players(int game_flag, int num)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
    Connection *con = mysql_instance->GetConnect();
	
    if (con == NULL)
    {
        GAME_LOG.error("get connect error.\n");
        return ret;
    }
    std::string sql;
	try
    {
        Statement *state =con->createStatement();  //��ȡ����״̬
        state->execute("use yxj_mjdb");
		char sz_sql[200]={0};
		sprintf(sz_sql, "insert into onlineNum_statistics set server_flag = %d, statistics_time = NOW(), num = %d;", game_flag, num);
		sql = sz_sql;		
        state->execute(sql);
        delete state; 
		state = NULL;
		ret = 0;
    }
    catch(sql::SQLException &e)
    {
         GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n",  sql.c_str(), e.what()); 
		 ret = -1;
    }
    catch(...)
    {
         GAME_LOG.error(" sql unknow error.\n");
		 ret = -1;
    }

	return ret;
}
*/

// 
// int init_db_player_flag(int game_flag)
// {
// 	int ret = -1;
// 	auto mysql_instance = MYSQL_CONN;
//     Connection *con = mysql_instance->GetConnect();
// 	
//     if (con == NULL)
//     {
//         GAME_LOG.error("get connect error.\n");
//         return ret;
//     }
//     std::string sql;
//     try
//     {
//         Statement *state =con->createStatement();  //获取连接状态
//         state->execute("use yxj_mjdb");
// 		char sz_sql[200]={0};
// 
// 		if (game_flag == 1)
// 		{
// 			sprintf(sz_sql, "update player set if_game = 0, if_online = 0 where if_game >0 and if_game < 1000000;");
// 		}
// 		else
// 		{
// 			sprintf(sz_sql, "update player set if_game = 0, if_online = 0 where if_game = %d;", game_flag);			
// 		}
// 		
// 		sql = sz_sql;		
//         state->execute(sql);
//         delete state; 
// 		state = NULL;
// 		ret = 0;
//     }
//     catch(sql::SQLException &e)
//     {
//          GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n",  sql.c_str(), e.what()); 
// 		 ret = -1;
//     }
//     catch(...)
//     {
//          GAME_LOG.error(" sql unknow error.\n");
// 		 ret = -1;
//     }
// 	return ret;
// }

int insert_board_end_record(
	int rec_id,
	int roomid,
	int board_choose,
	int cur_board,
	int player_num,
	std::string time_str,
	int end_type,
	int fangpaiji_mj,
	int next_fangpaiji_mj,
	int uid,
	std::string nick_name,
	int seatid,
	int if_banker,
	int jiaopai_flag,
	int cur_gang_score,
	int cur_angang_score,
	int cur_minggang_score,
	int cur_fanggang_score,
	int fangpaiji_score,
	int gudingji_score,
	int chongfengji_score,
	int zherenji_score,
	int cardtype_score,
	int winlose_score,
	int score,
	int cur_pao_flag,
	int card_type,
	int fangpaiji_num,
	std::string hole_str,
	std::string fm_str,
	std::string out_str
)
{
	int ret = -1;
    auto mysql_instance = MYSQL_CONN;
    Connection *con = mysql_instance->GetConnect();
    if (con == NULL)
    {
        GAME_LOG.error("get connect error.\n");
        return ret;
    }
	std::string sql = "INSERT INTO t_board_result_rec SET big_rec_id= ?,"
		"roomid = ?,"
		"board_choose=?,"
		"cur_board=?,"
		"player_num=?,"
		"time_str=?,"
		"end_type=?,"
		"fangpaiji_mj=?,"
		"next_fangpaiji_mj=?,"
		"uid=?,"
		"nick_name=?,"
		"seatid=?,"
		"if_banker=?,"
		"jiaopai_flag=?,"
		"cur_gang_score=?,"
		"cur_angang_score=?,"
		"cur_minggang_score=?,"
		"cur_fanggang_score=?,"
		"fangpaiji_score=?,"
		"gudingji_score=?,"
		"chongfengji_score=?,"
		"zherenji_score=?,"
		"cardtype_score=?,"
		"winlose_score=?,"
		"score=?,"
		"cur_pao_flag=?,"
		"card_type=?,"
		"fangpaiji_num=?,"
		"hole_str=?,"
		"fm_str=?,"
		"out_str=?;";
	stringstream out_sql;
	out_sql << sql
		<< "(p1)" << rec_id
		<< "(p2)" << roomid
		<< "(p3)" << board_choose
		<< "(p4)" << cur_board
		<< "(p5)" << player_num
		<< "(p6)" << time_str
		<< "(p7)" << end_type
		<< "(p8)" << fangpaiji_mj
		<< "(p9)" << next_fangpaiji_mj
		<< "(p10)" << uid
		<< "(p11)" << nick_name
		<< "(p12)" << seatid
		<< "(p13)" << if_banker
		<< "(p14)" << jiaopai_flag
		<< "(p15)" << cur_gang_score
		<< "(p16)" << cur_angang_score
		<< "(p17)" << cur_minggang_score
		<< "(p18)" << cur_fanggang_score
		<< "(p19)" << fangpaiji_score
		<< "(p20)" << gudingji_score
		<< "(p21)" << chongfengji_score
		<< "(p22)" << zherenji_score
		<< "(p23)" << cardtype_score
		<< "(p24)" << winlose_score
		<< "(p25)" << score
		<< "(p26)" << cur_pao_flag
		<< "(p27)" << card_type
		<< "(p28)" << fangpaiji_num
		<< "(p29)" << hole_str
		<< "(p30)" << fm_str
		<< "(p31)" << out_str;
	try
    {
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setUInt(1, rec_id);
		prep->setUInt(2, roomid);
		prep->setUInt(3, board_choose);
		prep->setUInt(4, cur_board);
		prep->setUInt(5, player_num);
		prep->setString(6, time_str);
		prep->setUInt(7, end_type);
		prep->setUInt(8, fangpaiji_mj);
		prep->setUInt(9, next_fangpaiji_mj);
		prep->setUInt(10, uid);
		prep->setString(11, nick_name);
		prep->setUInt(12, seatid);
		prep->setUInt(13, if_banker);
		prep->setUInt(14, jiaopai_flag);
		prep->setUInt(15, cur_gang_score);
		prep->setUInt(16, cur_angang_score);
		prep->setUInt(17, cur_minggang_score);
		prep->setUInt(18, cur_fanggang_score);
		prep->setUInt(19, fangpaiji_score);
		prep->setUInt(20, gudingji_score);
		prep->setUInt(21, chongfengji_score);
		prep->setUInt(22, zherenji_score);
		prep->setUInt(23, cardtype_score);
		prep->setUInt(24, winlose_score);
		prep->setUInt(25, score);
		prep->setUInt(26, cur_pao_flag);
		prep->setUInt(27, card_type);
		prep->setUInt(28, fangpaiji_num);
		prep->setString(29, hole_str);
		prep->setString(30, fm_str);
		prep->setString(31, out_str);
		if (prep->execute())
		{
			ret = 0;
		}
    }
    catch(sql::SQLException &e)
    {
		sql = out_sql.str();
         GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n",  sql.c_str(), e.what()); 
		 ret = -1;
    }
    catch(...)
    {
         GAME_LOG.error(" sql unknow error.\n");
		 ret = -1;
    }
	return ret;
}

int update_room_ticket_status(int roomid, int status)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
	Connection *con = mysql_instance->GetConnect();
	if (con == NULL)
	{
		GAME_LOG.error("get connect error.\n");
		return -1;
	}
	string sql;
	stringstream out_sql;
	sql = "UPDATE t_create_room_rec SET status = ? WHERE id = ?;";
	out_sql << sql << "(p1)" << status << "(p2)" << roomid;
	try
	{
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, status);
		prep->setInt(2, roomid);
		if (1 == prep->executeUpdate())
		{
			ret = 0;
		}
	}
	catch (sql::SQLException &e)
	{
		sql = out_sql.str();
		GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n", sql.c_str(), e.what());
		ret = -1;
	}
	catch (...)
	{
		GAME_LOG.error(" sql unknow error.\n");
		ret = -1;
	}

	return ret;
}

int bind_room_ticket(int roomid, int server_id, int desk_id)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
	Connection *con = mysql_instance->GetConnect();
	if (con == NULL)
	{
		GAME_LOG.error("get connect error.\n");
		return -1;
	}
	string sql;
	stringstream out_sql;
	sql = "UPDATE t_create_room_rec SET service_id = ?, desk_id = ? WHERE status = 2 AND id = ?;";
	out_sql << sql << "(p1)" << server_id << "(p2)" << desk_id << "(p2)" << roomid;
	try
	{
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, server_id);
		prep->setInt(2, desk_id);
		prep->setInt(3, roomid);
		if (1 == prep->executeUpdate())
		{
			ret = 0;
		}
	}
	catch (sql::SQLException &e)
	{
		sql = out_sql.str();
		GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n", sql.c_str(), e.what());
		ret = -1;
	}
	catch (...)
	{
		GAME_LOG.error(" sql unknow error.\n");
		ret = -1;
	}

	return ret;
}

int get_room_ticket_board_num(S_DB_TICKET_INFO & info, int roomid)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
	Connection *con = mysql_instance->GetConnect();
	if (con == NULL)
	{
		GAME_LOG.error("get connect error.\n");
		return -1;
	}
	string sql = "SELECT * FROM t_create_room_rec WHERE id = ? AND status <> 0 LIMIT 1;";
	stringstream out_sql;
	out_sql << sql << "(p1)" << roomid;
	try
	{
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, roomid);
		ResultSet* result = prep->executeQuery();
		if (result->next())
		{
			ret = 0;
			info.clubid = result->getInt("clubid");
			info.uid = result->getInt("uid");
			info.player_num = result->getInt("player_num");
			info.board_num = result->getInt("board_choose");
			info.status = result->getInt("status");
			info.pwd = result->getInt("password");
		}
		if (result)delete result;
	}
	catch (sql::SQLException &e)
	{
		sql = out_sql.str();
		GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n", sql.c_str(), e.what());
		ret = -1;
	}
	catch (...)
	{
		GAME_LOG.error(" sql unknow error.\n");
		ret = -1;
	}

	return ret;
}

int insert_total_end_record(int roomid,int board_choose, int end_board, std::string time_str,
	int uid1, int score1,
	int uid2, int score2,
	int uid3, int score3,
	int uid4, int score4)
{
	int ret = -1;
    auto mysql_instance = MYSQL_CONN;
    Connection *con = mysql_instance->GetConnect();
    if (con == NULL)
    {
        GAME_LOG.error("get connect error.\n");
        return ret;
    }
	std::string sql = "INSERT INTO t_room_result_rec SET id = ?,"
		"board_choose=?,end_board=?,"
		"begin_time=?,"
		"uid1=?,score1=?,"
		"uid2=?,score2=?,"
		"uid3=?,score3=?,"
		"uid4=?,score4=?;";
	stringstream out_sql;
	out_sql << sql
		<< "(p1)" << roomid
		<< "(p2)" << board_choose
		<< "(p3)" << end_board
		<< "(p4)" << time_str
		<< "(p5)" << uid1
		<< "(p6)" << score1
		<< "(p7)" << uid2
		<< "(p8)" << score2
		<< "(p9)" << uid3
		<< "(p10)" << score3
		<< "(p11)" << uid4
		<< "(p12)" << score4;

	try
    {
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, roomid);
		prep->setInt(2, board_choose);
		prep->setInt(3, end_board);
		prep->setDateTime(4, time_str);
		prep->setInt(5, uid1);
		prep->setInt(6, score1);
		prep->setInt(7, uid2);
		prep->setInt(8, score2);
		prep->setInt(9, uid3);
		prep->setInt(10, score3);
		prep->setInt(11, uid4);
		prep->setInt(12, score4);
		if (prep->execute())
		{
			ret = 0;
		}

    }
    catch(sql::SQLException &e)
    {
		sql = out_sql.str();
         GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n",  sql.c_str(), e.what()); 
		 ret = -1;
    }
    catch(...)
    {
         GAME_LOG.error(" sql unknow error.\n");
		 ret = -1;
    }
	
	return ret;
}

int update_total_end_record(int rec_id, int end_board,std::string time_str,int score1,int score2,int score3,int score4)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
	Connection *con = mysql_instance->GetConnect();
	if (con == NULL)
	{
		GAME_LOG.error("get connect error.\n");
		return ret;
	}
	std::string sql = "UPDATE t_room_result_rec SET "
		"end_board=?,"
		"score1=?,"
		"score2=?,"
		"score3=?,"
		"score4=?"
		" WHERE id = ?;";
	stringstream out_sql;
	out_sql << sql
		<< "(p1)" << end_board
		<< "(p2)" << score1
		<< "(p3)" << score2
		<< "(p4)" << score3
		<< "(p5)" << score4
		<< "(p6)" << rec_id;

	try
	{
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, end_board);
		prep->setInt(2, score1);
		prep->setInt(3, score2);
		prep->setInt(4, score3);
		prep->setInt(5, score4);
		prep->setInt(6, rec_id);
		if (1 == prep->executeUpdate())
		{
			ret = 0;
		}

	}
	catch (sql::SQLException &e)
	{
		sql = out_sql.str();
		GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n", sql.c_str(), e.what());
		ret = -1;
	}
	catch (...)
	{
		GAME_LOG.error(" sql unknow error.\n");
		ret = -1;
	}

	return ret;
}

int increase_user_result(int uid, int result)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
	Connection *con = mysql_instance->GetConnect();

	if (con == NULL)
	{
		GAME_LOG.error("get connect error.\n");
		return ret;
	}
	std::string sql;
	if (result == 1)//win
	{
		sql = "UPDATE t_player_info SET win_board = win_board + 1 WHERE id = ?;";
	}
	else if (result == 2)//fail
	{
		sql = "UPDATE t_player_info SET fail_board = fail_board + 1 WHERE id = ?;";
	}
	else if (result == 3)//peace
	{
		sql = "UPDATE t_player_info SET peace_board = peace_board + 1 WHERE id = ?;";
	}
	else
	{
		return ret;
	}
	stringstream out_sql;
	out_sql << sql
		<< "(p1)" << uid;
	try
	{
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, uid);
		if (prep->execute())
		{
			ret = 0;
		}
	}
	catch (sql::SQLException &e)
	{
		sql = out_sql.str();
		GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n", sql.c_str(), e.what());
		ret = -1;
	}
	catch (...)
	{
		GAME_LOG.error(" sql unknow error.\n");
		ret = -1;
	}

	return ret;
}

int increase_add_action(int server_id, std::string date)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
	Connection *con = mysql_instance->GetConnect();

	if (con == NULL)
	{
		GAME_LOG.error("get connect error.\n");
		return ret;
	}
	std::string sql = "INSERT INTO t_server_rec SET "
		"server_id = ?,date = ? ON DUPLICATE KEY UPDATE num = num + 1;";
	stringstream out_sql;
	out_sql << sql
		<< "(p1)" << server_id
		<< "(p2)" << date;
	try
	{
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, server_id);
		prep->setString(2, date);
		if (prep->execute())
		{
			ret = 0;
		}
	}
	catch (sql::SQLException &e)
	{
		sql = out_sql.str();
		GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n", sql.c_str(), e.what());
		ret = -1;
	}
	catch (...)
	{
		GAME_LOG.error(" sql unknow error.\n");
		ret = -1;
	}

	return ret;
}

int insert_diamonds_change_rec(int uid, int change, std::string source, std::string comment)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
    Connection *con = mysql_instance->GetConnect();
	
    if (con == NULL)
    {
        GAME_LOG.error("get connect error.\n");
        return ret;
    }
	std::string sql = "INSERT INTO t_grant_diamonds_rec SET "
		"uid=?,num=?,source=?,comment=?;";
	stringstream out_sql;
	out_sql << sql
		<< "(p1)" << uid
		<< "(p2)" << change
		<< "(p3)" << source
		<< "(p4)" << comment;
	try
    {
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, uid);
		prep->setInt(2, change);
		prep->setString(3, source);
		prep->setString(4, comment);
		if (prep->execute())
		{
			ret = 0;
		}
    }
    catch(sql::SQLException &e)
    {
		sql = out_sql.str();
         GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n",  sql.c_str(), e.what()); 
		 ret = -1;
    }
    catch(...)
    {
         GAME_LOG.error(" sql unknow error.\n");
		 ret = -1;
    }
	
	return ret;
}


int insert_gold_change_rec(int uid, int change, std::string source, std::string comment)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
	Connection *con = mysql_instance->GetConnect();

	if (con == NULL)
	{
		GAME_LOG.error("get connect error.\n");
		return ret;
	}
	std::string sql = "INSERT INTO t_grant_gold_rec SET "
		"uid=?,num=?,source=?,comment=?;";
	stringstream out_sql;
	out_sql << sql
		<< "(p1)" << uid
		<< "(p2)" << change
		<< "(p3)" << source
		<< "(p4)" << comment;
	try
	{
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, uid);
		prep->setInt(2, change);
		prep->setString(3, source);
		prep->setString(4, comment);
		if (prep->execute())
		{
			ret = 0;
		}
	}
	catch (sql::SQLException &e)
	{
		sql = out_sql.str();
		GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n", sql.c_str(), e.what());
		ret = -1;
	}
	catch (...)
	{
		GAME_LOG.error(" sql unknow error.\n");
		ret = -1;
	}

	return ret;
}

int update_gold_room_player_count(int server_id, int online_num)
{
	int ret = -1;
	auto mysql_instance = MYSQL_CONN;
    Connection *con = mysql_instance->GetConnect();
	
    if (con == NULL)
    {
        GAME_LOG.error("get connect error.\n");
        return ret;
    }
    std::string sql = "UPDATE t_server_info SET online_num = ? WHERE id = ?;";
	stringstream out_sql;
	out_sql << sql
		<< "(p1)" << online_num
		<< "(p2)" << server_id;
	try
    {
		con->setSchema(YXJMJ_CONF._default_db);
		auto prep = mysql_instance->GetPrepare(sql);
		prep->setInt(1, online_num);
		prep->setInt(2, server_id);
		if (1 == prep->executeUpdate())
		{
			ret = 0;
		}
    }
    catch(sql::SQLException &e)
    {
		sql = out_sql.str();
         GAME_LOG.error(" exec sql: [%s], error, error info: [%s].\n",  sql.c_str(), e.what()); 
		 ret = -1;
    }
    catch(...)
    {
         GAME_LOG.error(" sql unknow error.\n");
		 ret = -1;
    }

	return ret;

}

int init_gold_room_player_count(int server_id)
{
	return update_gold_room_player_count(server_id, 0);
}


